// pages/map/map.js
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
 // 地理编码，逆地址解析
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lat: '',
    lng: '',
    markers:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: 'ZPIBZ-RFURV-KSCP3-UDX4W-JZEHJ-3SBCM'
    });
   
    let _this = this;
    wx.getLocation({
      type: 'gcj02',
      success: function(res) {
        _this.setData({
          lng: res.longitude,
          lat: res.latitude
        })
      },
    })
  },
  onShow() {
    let _this = this;
    qqmapsdk.search({
      keyword: '酒店',
      success: function(res) {
        console.log(res);
        let allAdd = res.data || [];
        let markers = [];
        allAdd.forEach(add =>{
          markers.push({
            id:add.id,
            latitude: add.location.lat,
            longitude: add.location.lng,
            iconPath:'/images/pos.png',
            callout:{
              content:add.title,
              display:'BYCLICK',
              padding:10,
              bgColor:'#e2e2e2',
              borderRadius:8
            }
          })
        })
        console.log(markers)
        _this.setData({
          markers
        })
      },
      fail: function(res) {
        console.log(res);
      },
      complete: function(res) {
        console.log(res);
      }
    })
  },
  clickMap(e) {
    console.log(e)
  },
  clickMarker(e){
    console.log(e)
  },
  clickCallout(e){
    console.log(e)
  }
})